//
//  PlayViewController.m
//  EconomyDemo
//
//  Created by qianfeng01 on 15/7/12.
//  Copyright (c) 2015年 xiangmu. All rights reserved.
//

#import "PlayViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface PlayViewController ()
{
    MPMoviePlayerViewController *_mp;
}
@end

@implementation PlayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatMoviePlayer];
}
- (void)dealloc {
    if (_mp) {
        [_mp.moviePlayer stop];
    }
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}

- (void)creatMoviePlayer {
    _mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:_url]];
    _mp.view.frame = self.view.frame;
    
    [self.view addSubview:_mp.view];
    _mp.moviePlayer.shouldAutoplay = YES;
    [_mp.moviePlayer play];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playBack:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    
}
- (void)playBack:(NSNotification *)nf{
    NSDictionary *dict = nf.userInfo;
    
    NSInteger type = [dict[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
    switch (type) {
        case 1:
        {
            NSLog(@"自动播放完毕");
        }
            break;
        case 2:
        {
            [_mp.moviePlayer stop];
            [self dismissViewControllerAnimated:YES completion:nil];
            
        }
            break;
        case 3:
        {
            NSLog(@"播放有异常");
        }
            break;
            
        default:
            break;
    }
    
}

@end
